#!/usr/bin/env /usr/local/python3
# -*- coding: utf-8 -*-

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

"""
人脸识别
"""

__author__ = "hubert"


if __name__ == '__main__':
    cap = cv.VideoCapture("/Users/hubert/Downloads/视频测试/part1.ts")  # rtsp://admin:admin@192.168.2.64:554//Streaming/Channels/1
    # 灰度
    # img_ex = cv2.imread("/Users/hubert/Downloads/视频测试/example.jpg", 0)

    img_ex = cv.imread("/Users/hubert/Downloads/视频测试/example.jpg")

    # 读取视频
    while cap.isOpened():
        retval, image = cap.read()
        if not retval:  # 读完视频后falg返回False
            print("播放完毕，退出")
            break
        print("时间：", cap.get(0))  # cap.get(0) 视频文件的当前位置（播放）以毫秒为单位
        # """
        if retval:

            # 灰度处理1
            gray1 = cv.cvtColor(img_ex, cv.COLOR_BGR2GRAY)

            gray2 = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

            min_hessian = 1000
            sift = cv.SIFT_create(min_hessian)

            # 分别计算特征点和特征描述符，此处采用sift方法
            keypoints1, features1 = sift.detectAndCompute(gray1, None)
            keypoints2, features2 = sift.detectAndCompute(gray2, None)

            # 画特征点
            kpImg1 = cv.drawKeypoints(gray1, keypoints1, img_ex)
            kpImg2 = cv.drawKeypoints(gray2, keypoints2, image)
            bf = cv.BFMatcher()
            matches = bf.knnMatch(features1, features2, k=2)

            good = []
            for m, n in matches:
                if m.distance < 0.75*n.distance:
                    good.append([m])
                    print([m])

            # 用drawMatchesKnn画出匹配状态并将结果输出resultImg一个输出灰度图  一个输出彩图

            frame = cv.drawMatchesKnn(gray1, keypoints1, gray2, keypoints2, good, None, flags=2)

            # resultImg1 = cv2.drawMatchesKnn(imageA, keypointsA, imageB, keypointsB, good,None, flags=2)

            # plt.imshow(resultImg),plt.show()

            cv.resize(frame, None, fx=0.5, fy=0.5)
            # 显示视频窗
            cv.imshow("Video_Frame", frame)
        # """
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    cv.destroyAllWindows()
    cap.release()





